package com.glory.cqaospda.model

import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.bean.*
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_VersionEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import rxhttp.awaitResult
import rxhttp.onErrorReturnItem
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class TaskRepository : BaseRepository() {

    /**
     * 登录
     * */
    suspend fun login(map: JsonObject, url: String): LogInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")
        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_UserLogIn"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<LogInfoBean>()
            .await()
    }

    /**
     * 获取批次信息
     */
    suspend fun defaultLotInfo(map: JsonObject,url:String): LotInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_GetLotInfo"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<LotInfoBean>()
            .await()
    }

    /**
     * EndRun
     */
    suspend fun defaultEndRun(map: JsonObject,url:String): EndRunBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_EndRun"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<EndRunBean>()
            .await()
    }
    /**
     * 批次出战
     */
    suspend fun defaultBatchTrackOut(map: JsonObject,url:String): BatchTrackOutBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_BatchTrackOut"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<BatchTrackOutBean>()
            .await()
    }

    /**
     * 单批次作业
     */
    suspend fun defaultRunLotHis(map: JsonObject,url:String): RunLotHiBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_RunLotHis"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<RunLotHiBean>()
            .await()
    }

    /**
     * 获取设备信息
     */
    suspend fun defaultEqpInfo(map: JsonObject,url:String): EqpInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_GetEqpInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<EqpInfoBean>()
            .await()
    }

    /**
     * 获取设备物料信息
     */
    suspend fun defaultEqpMaterials(map: JsonObject,url:String): EqpMaterialsBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_EqpMaterials"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<EqpMaterialsBean>()
            .await()
    }

    /**
     * 获取批次物料信息
     */
    suspend fun defaultBOM(map: JsonObject,url:String): BomBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_BOM"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<BomBean>()
            .await()
    }

    /**
     * 开始作业
     */
    suspend fun defaultBatchTrackIn(map: JsonObject,url:String): TrackInInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<TrackInInfoBean>()
            .await()
    }

    /**
     * 更换物料
     */
    suspend fun defaultEquipmentConversion(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_EquipmentConversion"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 获取批次信息
     */
    suspend fun defaultFinalTestProgram(map: JsonObject,url:String): FinalTestProgramBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "FinalTestProgram"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<FinalTestProgramBean>()
            .await()
    }

    /**
     * 提交维修
     */
    suspend fun defaultRepair(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "Repair"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 获取维修子项
     */
    suspend fun defaultGetBaseConfig(map: JsonObject,url:String): BaseConfigBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "GetBaseConfig"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<BaseConfigBean>()
            .await()
    }

    /**
     * 获取维修列表
     */
    suspend fun defaultGetMaintenance(map: JsonObject,url:String): MaintenanceBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "Maintenance"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<MaintenanceBean>()
            .await()
    }

    /**
     * 维修提交，维修状态更改
     */
    suspend fun defaultMaintenanceOrder(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "MaintenanceOrder"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 送检
     */
    suspend fun defaultSendCheck(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "SendCheck"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    suspend fun defaultSpotCheckInfo(map: JsonObject,url:String): SpotCheckInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_SpotCheckInfo"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<SpotCheckInfoBean>()
            .await()
    }

    suspend fun defaultCancelBuyoff(map: JsonObject,url:String): SpotCheckInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_CancelBuyoff"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<SpotCheckInfoBean>()
            .await()
    }

    /**
     * 获取送检列表
     */
    suspend fun defaultProcessCheck(map: JsonObject,url:String): ProcessCheckBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "ProcessCheck"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<ProcessCheckBean>()
            .await()
    }

    /**
     * 点检提交，点检状态更改
     */
    suspend fun defaultCheckOrder(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "CheckOrder"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 异常结束
     */
    suspend fun defaultAbnormalTrack(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "AbnormalTrack"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 提交待料、待配件
     */
    suspend fun defaultWaitMaterial(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "WaitMaterial"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 获取设备状态
     */
    suspend fun defaultGetEqpStatus(map: JsonObject,url:String): EqpStatusBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "GetEqpStatus"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<EqpStatusBean>()
            .await()
    }

    /**
     * 交接班
     */
    suspend fun defaultShift(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_Shift"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * PM
     */
    suspend fun defaultPM(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_PM"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * PM获取设备状态
     */
    suspend fun defaultGetPMStatus(map: JsonObject,url:String): PMStatusBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_GetPMStatus"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<PMStatusBean>()
            .await()
    }

    /**
     * 查询设备信息
     */
    suspend fun defaultQueryEqpInfo(map: JsonObject,url:String): QueryEqpInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_QueryEqpInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<QueryEqpInfoBean>()
            .await()
    }

    /**
     * 查询设备信息
     */
    suspend fun defaultQueryRepairInfo(map: JsonObject,url:String): QueryRepairInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_QueryRepairInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<QueryRepairInfoBean>()
            .await()
    }

    /**
     * 查询生产信息
     */
    suspend fun defaultQueryProductionInfo(map: JsonObject,url:String): QueryProductionInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_QueryProductionInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<QueryProductionInfoBean>()
            .await()
    }

    /**
     * 工号录入
     * */
    suspend fun defaultUserInfoEntry(map: JsonObject, url: String): UserInfoEntry {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")
        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_UserInfoEntry"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<UserInfoEntry>()
            .await()
    }

    /**
     * 状态切换
     * */
    suspend fun defaultStatusChange(map: JsonObject, url: String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")
        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 空过站
     */
    suspend fun defaultPassSationEnd(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_PassSationEnd"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 获取待料待配件信息
     */
    suspend fun defaultWaitMaterialInfo(map: JsonObject,url:String): WaitMaterialInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_WaitMaterialInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<WaitMaterialInfoBean>()
            .await()
    }

    /**
     * 查询备件寿命
     */
    suspend fun defaultQuerySparePartsLife(map: JsonObject,url:String): QueryRaptsInfoBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_RaptsInfo"))
            .connectTimeout(Constant.connectTimeout * 1000)
            .toResponse<QueryRaptsInfoBean>()
            .await()
    }

    /**
     * 批次出战
     */
    suspend fun defaultLotSwitch(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_LotSwitch"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    suspend fun defaultGetAccount(map: JsonObject,url:String): AccountBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<AccountBean>()
            .await()
    }

    suspend fun defaultGetResultData(map: JsonObject,url:String): ResultData {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, "EMS_GetResultData"))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<ResultData>()
            .await()
    }

    /**
     * 重新加载
     */
    suspend fun defaultProgramReload(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 信息录入
     */
    suspend fun defaultPeelingForce(map: JsonObject,url:String): Any {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<Any>()
            .await()
    }

    /**
     * 获取批次失效信息
     */
    suspend fun defaultGetMesRejectData(map: JsonObject,url:String): MesRejectBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, url))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<MesRejectBean>()
            .await()
    }

    /**
     * 获取真空信息
     */
    suspend fun defaultGetQTIMEData(map: JsonObject,url:String,message:String): QTimeBean {
        FreeApi_LogUtils.saveErrorMessages(Constant.http + Constant.mUrl + Constant.AOSPDA + url,"请求地址：")

        return RxHttp.postJson(Constant.http + Constant.mUrl + Constant.AOSPDA + url)
            .addAll(getInJsonData(map, message))
            .readTimeout(Constant.connectTimeout * 1000)
            .toResponse<QTimeBean>()
            .await()
    }
/**
     * 获取APP更新地址
     * */

    suspend fun defaultUpdate(map: Map<String, String>): FreeUI_VersionEntity {
    FreeApi_LogUtils.saveErrorMessages("http://"+Constant.fileUrl+"/Files/PDA/PDA.txt","更新请求：")

    return RxHttp.get("http://"+Constant.fileUrl+"/Files/PDA/PDA.txt")
            .addAll(map)
            .toClass<FreeUI_VersionEntity>()
            .await()
    }



}